import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '@/LayOut'
import { isLogin } from '@/utils/indexLogin'

const routes = [
  {
    path: '/',
    name: 'layout',
    component: LayOut
    // children: [
    //   {
    //     path: '/',
    //     redirect: '/dashBoard'
    //   },
    //   {
    //     path: '/dashBoard',
    //     name: 'dashBoard',
    //     component: () => import('@v/DashBoard')
    //   },
    //   {
    //     path: '/itemLists',
    //     name: 'itemLists',
    //     component: () => import('@v/ItemLists')
    //   },
    //   {
    //     path: '/itemAdd',
    //     name: 'itemAdd',
    //     component: () => import('@v/ItemAdd')
    //   },
    //   {
    //     path: '/itemUpdate',
    //     name: 'itemUpdate',
    //     component: () => import('@v/ItemUpdate')
    //   },
    //   {
    //     path: '/cateLists',
    //     name: 'cateLists',
    //     component: () => import('@v/CateLists')
    //   },
    //   {
    //     path: '/userInfo',
    //     name: 'userInfo',
    //     component: () => import('@v/UserInfo')
    //   },
    //   {
    //     path: '/setting',
    //     name: 'setting',
    //     component: () => import('@v/SettingPage')
    //   }
    // ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@v/LoginPage')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: () => import('@v/NotFound')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (to.path !== '/login') {
    if (isLogin()) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
